package com.carcenter.personnel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.carcenter.personnel.entity.Permissions;
import com.carcenter.common.pojo.personnel.PermissionsVo;
import com.carcenter.personnel.mapper.PermissionsMapper;
import com.carcenter.personnel.service.IPermissionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author car
 * @since 2025-07-16
 */
@Service
public class PermissionsServiceImpl extends ServiceImpl<PermissionsMapper, Permissions> implements IPermissionsService {

    @Autowired
    private PermissionsMapper permissionsMapper;

    @Override
    public List<PermissionsVo> getAllpermissions() {
        List<PermissionsVo> firstPermissions = getPermissionsByParentId(0);
        for (PermissionsVo permissionsVo : firstPermissions) {
            List<PermissionsVo> secondPermissions = getPermissionsByParentId(permissionsVo.getId());
            permissionsVo.setChildren(secondPermissions);
        }
        return firstPermissions;
    }

    public List<PermissionsVo> getPermissionsByParentId(Integer parentId) {
        LambdaQueryWrapper<Permissions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permissions::getParentId, parentId);
        List<Permissions> permissionsList = permissionsMapper.selectList(queryWrapper);
        List<PermissionsVo> permissionsVos = new ArrayList<>();
        for (Permissions permissions : permissionsList) {
            PermissionsVo permissionsVo = new PermissionsVo();
            permissionsVo.setId(permissions.getId());
            permissionsVo.setParentId(permissions.getParentId());
            permissionsVo.setPermissionName(permissions.getPermissionName());
            permissionsVos.add(permissionsVo);
        }
        return permissionsVos;
    }
}
